package br.com.wai2008.client.wireless;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.image.ImageObserver;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import br.com.wai2008.client.protocol.ApplyProtocol;

public class WAISignalsGraphic extends Panel {

	private static final long serialVersionUID = -1563713751792648611L;

    static int xini = 27;
    static int xpos = xini;
    static int xend = 822;
    static int numeroPontos = 100;
    static int xsum = (xend-xini)/numeroPontos;
	
	public static List<List> signals = new ArrayList<List>();
	public static List<Integer> xinis = new ArrayList<Integer>();
	public static HashMap<String, List> pontos = new HashMap<String, List>();
	
	private static CanvasGraphic graphic;

	public static void setSignals(BufferedReader buffer) {
		String[] sinais = ApplyProtocol.buildWirelessSignalMessage(buffer);
		if (sinais != null) {
			for (int i=0; i < sinais.length; i = i+2) {
				String id = sinais[i];
				int sinal = new Integer(sinais[i+1]);
				boolean inserted = false;
				for (int j=0; j < signals.size(); j++) {
					if (((String)signals.get(j).get(0)).compareTo(id) == 0) {
						signals.get(j).add(sinal);
						pontos.get(id).add(xpos);
						inserted = true;
						break;
					}
				}
				if (!inserted) {
					List list = new ArrayList();
					list.add(id);
					list.add(sinal);
					List list2 = new ArrayList();
					list2.add(xpos);
					pontos.put(id, list2);
					signals.add(list);
				}
			}
		}
		xpos += xsum;
	}
	
	public WAISignalsGraphic(WAISignalsInterface signal) {
		graphic = new CanvasGraphic(signal);
        add(graphic);
	}
	
}

class CanvasGraphic extends Canvas implements ImageObserver, Runnable {

	private static final long serialVersionUID = -6463084248902268122L;

	public static Color[] color = new Color[]{Color.red, Color.blue, Color.orange, 
		Color.pink, Color.magenta, Color.cyan, Color.black, Color.darkGray, 
		Color.lightGray, Color.green, Color.yellow, Color.white};
	
    Image img;
    WAISignalsInterface app;
    
    int yini = 60;
    int yend = 431;
    double ymed = (yend - yini)/10;
    
    public CanvasGraphic(WAISignalsInterface signal) {
    	this.app = signal;
        setBounds(0, 0, 850, 800);
        new Thread(this).start();
    }
    
    public void reset() {
    	WAISignalsGraphic.signals = new ArrayList<List>();
    	WAISignalsGraphic.xinis = new ArrayList<Integer>();
    	WAISignalsGraphic.pontos = new HashMap<String, List>();
    	WAISignalsGraphic.xpos = WAISignalsGraphic.xini;
    }

    public void paint(Graphics g) {
		g.drawImage(img, 0, 0, this);
        Iterator it = WAISignalsGraphic.signals.iterator();
    	int k = 0;
        while (it.hasNext()) {
            g.setColor(color[k%12]);
        	List list = (List) it.next();
        	if (list.size() == 2) {
        		int y1 = (Integer) list.get(1);
        		System.out.print("y: " + y1 + "  ");
        		if (y1 != 0) {
        			y1 = (y1 + 100);
        			double z = (new Double(y1) / 10) * ymed;
        			y1 = new Double(z).intValue() + yini;
        		} else {
        			y1 = yend;
        		}
    			int x1 = (Integer) WAISignalsGraphic.pontos.get((String) list.get(0)).get(0);
    			if (x1 > WAISignalsGraphic.xend) {
    				this.reset();
    				return;
    			}
        		System.out.println("1: " + x1 + " " + y1 + "   2: " + x1 + " " + y1);
    			g.drawLine(x1, y1, x1, y1);
        	} else if (list.size() > 2){
	        	for (int i = 1; i < list.size()-1; i++) {
	        		int y1 = (Integer) list.get(i);
	        		int y2 = (Integer) list.get(i+1);
	        		System.out.print("y1: " + y1 + " y2: " + y2 + "  ");
	        		if (y1 != 0) {
	        			y1 = (y1 + 100);
	        			double z = (new Double(y1) / 10) * ymed;
	        			y1 = new Double(z).intValue() + yini;
	        		} else {
	        			y1 = yend;
	        		}
	        		int x1 = (Integer) WAISignalsGraphic.pontos.get((String) list.get(0)).get(i-1);
	        		if (y2 != 0) {
	        			y2 = (y2 + 100);
	        			double z = (new Double(y2) / 10) * ymed;
	        			y2 = new Double(z).intValue() + yini;
	        		} else {
	        			y2 = yend;
	        		}
	        		int x2 = (Integer) WAISignalsGraphic.pontos.get((String) list.get(0)).get(i);
	    			if (x1 > WAISignalsGraphic.xend || x2 > WAISignalsGraphic.xend) {
	    				this.reset();
	    				return;
	    			}
	        		System.out.println("1: " + x1 + " " + y1 + "   2: " + x2 + " " + y2);
	        		if ((x2-x1) == WAISignalsGraphic.xsum) {
	        			g.drawLine(x1, y1, x2, y2);
	        		} else {
	        			g.drawLine(x1, y1, x1, y1);
	        		}
	        	}
        	}
        	k++;
        }
    }
	
	@Override
	public void run() {
		img = app.getImage(app.getDocumentBase(), "br/com/wai2008/client/wireless/medida.png");
		while(true) {
			WirelessSignal.getSignal();
	        repaint();
            try {
                Thread.sleep(700);
            } catch (InterruptedException e) {
            }
		}
	}

}
